package handlers

import (
	"github.com/emicklei/go-restful"
	"github.com/gorilla/websocket"
	"kubeInstall/pkg/logger"
	"time"
)

type IBaseHandler interface {
	AddToRoute(ws *restful.WebService)
}

type BaseHandler struct {
}

func (b BaseHandler) AddToRoute(ws *restful.WebService) {
	panic("do not implement me")
}

func (b *BaseHandler) RenderError(w *restful.Response, ErrorMessage string, statusCode int) {
	var data Error

	data.StatusCode = statusCode
	data.ErrorMessage = ErrorMessage
	logger.Error(ErrorMessage)
	w.WriteHeaderAndJson(statusCode, data, restful.MIME_JSON)

}
func (b *BaseHandler) HandleWsError(ws *websocket.Conn, err error) bool {
	if err != nil {
		logger.Error("handler ws ERROR:")
		dt := time.Now().Add(time.Second)
		if err := ws.WriteControl(websocket.CloseMessage, []byte(err.Error()), dt); err != nil {
			logger.Error("websocket writes control message failed: %v",err)
		}
		return true
	}
	return false
}
func (b *BaseHandler) RenderResp(resp *restful.Response, respData interface{}, statusCode int) {

	resp.WriteHeaderAndJson(statusCode, respData, restful.MIME_JSON)

}
